package org.adw.launcher2.ld.secret.dom;

import java.util.List;

import org.adw.launcher2.ld.secret.db.MyContactCache;
import org.adw.launcher2.ld.secret.dom.MyData;
import org.adw.launcher2.ld.secret.dom.contact.*;
import org.adw.launcher2.ld.secret.ui.utils.Global;

import android.content.ContentResolver;
import android.database.sqlite.SQLiteConstraintException;


public class MyContact extends MyData{// This class contain all info of 1 user (UserInfo, ImInfo, Org, etc)
	private UserInfo userInfo;
	private List<PhoneInfo> listPhone;
	private List<EmailInfo> listEmail;
	private List<PostalAddressInfo> listPA;
	private List<OrganizationInfo> listOrg;
	private List<ImInfo> listIm;
	private List<WebsiteInfo> listWeb;
	private ContactAccessor mContactAccessor;
	public MyContact(UserInfo userInfo, List<PhoneInfo> listPhone, List<EmailInfo> listEmail,
			List<PostalAddressInfo> listPA, List<OrganizationInfo> listOrg, List<ImInfo> listIm,
			List<WebsiteInfo> listWeb){
		mContactAccessor = new ContactAccessor(); 
		this.userInfo = userInfo;
		this.listPhone = listPhone;
		this.listEmail = listEmail;
		this.listPA = listPA;
		this.listOrg = listOrg;
		this.listIm = listIm;
		this.listWeb = listWeb;
		
	}
	public MyContact(){
		mContactAccessor = new ContactAccessor(); 
	};
	public UserInfo getUserInfo(){
		return this.userInfo;
	}
	public List<PhoneInfo> getListPhone(){
		return this.listPhone;
	}
	public List<EmailInfo> getListEmail(){
		return this.listEmail;
	}
	public List<PostalAddressInfo> getListPA(){
		return this.listPA;
	}
	public List<OrganizationInfo> getListOrg(){
		return this.listOrg;
	}
	public List<ImInfo> getListIm(){
		return this.listIm;
	}
	public List<WebsiteInfo> getListWeb(){
		return this.listWeb;
	}
	@Override
	public void hide() {
		ContentResolver cr = Global.cr;
		UserInfo user = mContactAccessor.loadUserInfos(cr, id);
		List<PhoneInfo> phone = mContactAccessor.loadUserPhones(cr, id);
		List<EmailInfo> email = mContactAccessor.loadUserEmails(cr, id);
		List<OrganizationInfo> org = mContactAccessor.loadUserOrg(cr, id);
		List<ImInfo> im = mContactAccessor.loadUserIm(cr, id);
		List<PostalAddressInfo> pa = mContactAccessor.loadUserPA(cr, id);
		List<WebsiteInfo> web = mContactAccessor.loadUserWebsite(cr, id);
		MyContact myContact = new MyContact(user, phone, email, pa, org, im, web);
		// insert into secret DB
		try{
			MyContactCache cache = MyContactCache.getInstance(Global.dbHelper);
			cache.insertContactToCache(myContact);
		}catch(SQLiteConstraintException ex){
			ex.printStackTrace();
		}

		mContactAccessor.deleteUser(cr, user.getUserId());
	}
	@Override
	public void restore() {
		ContentResolver cr = Global.cr;
		MyContactCache cache = MyContactCache.getInstance(Global.dbHelper);
		MyContact myContact = cache.getContactById(id);
		mContactAccessor.insertAllInfoOfOneUser
		(cr, myContact.getUserInfo(), myContact.getListPhone(), myContact.getListEmail(),
				myContact.getListPA(), myContact.getListOrg(), myContact.getListIm(), myContact.getListWeb());
//		Log.d("Restore", myContact.getUserInfo().getUserName());
//		Log.d("MYCONTACT: ID RESTORE", id+"");
		cache.deleteUser(id);
	}
}
